/**
 * Created by 杨永安 on 2017/4/18.
 */
initRongCloud()


var RongY = {
    count: 0, normal: 0
}

function initRongCloud() {


    $.get('/Rong', function (ret) {


        if (ret.code == 200) {
            // 初始化。
            RongIMClient.init("6tnym1brnog87");
            var token = ret.token;
            // 连接融云服务器。
            RongIMClient.connect(token, {
                onSuccess: function (userId) {
                    console.log("Login successfully." + userId);
                },
                onTokenIncorrect: function () {
                    console.log('token无效');
                },
                onError: function (errorCode) {
                    var info = '';
                    switch (errorCode) {
                        case RongIMLib.ErrorCode.TIMEOUT:
                            info = '超时';
                            break;
                        case RongIMLib.ErrorCode.UNKNOWN_ERROR:
                            info = '未知错误';
                            break;
                        case RongIMLib.ErrorCode.UNACCEPTABLE_PaROTOCOL_VERSION:
                            info = '不可接受的协议版本';
                            break;
                        case RongIMLib.ErrorCode.IDENTIFIER_REJECTED:
                            info = 'appkey不正确';
                            break;
                        case RongIMLib.ErrorCode.SERVER_UNAVAILABLE:
                            info = '服务器不可用';
                            break;
                    }
                    console.log(errorCode);
                }
            });
        }
    })


}


// 设置连接监听状态 （ status 标识当前连接状态）
// 连接状态监听器
RongIMClient.setConnectionStatusListener({
    onChanged: function (status) {
        switch (status) {
            //链接成功
            case RongIMLib.ConnectionStatus.CONNECTED:
                console.log('链接成功');
                break;
            //正在链接
            case RongIMLib.ConnectionStatus.CONNECTING:
                console.log('正在链接');
                break;
            //重新链接
            case RongIMLib.ConnectionStatus.DISCONNECTED:
                console.log('断开连接');
                break;
            //其他设备登录
            case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
                console.log('其他设备登录');
                $.confirm("您的账号在其他地方登录,是否重新登录?")
                break;
            //网络不可用
            case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
                console.log('网络不可用');
                break;
        }
    }
});

// 消息监听器
RongIMClient.setOnReceiveMessageListener({
    // 接收到的消息
    onReceived: function (message) {
        // 判断消息类型
        switch (message.messageType) {
            case RongIMClient.MessageType.TextMessage:


                clearTimeout(window.t);

                window.t = setTimeout(function () {
                    handleMsg(message, RongIMClient)
                }, 500);


                break;
            case RongIMClient.MessageType.VoiceMessage:
                // 对声音进行预加载
                // message.content.content 格式为 AMR 格式的 base64 码
                RongIMLib.RongIMVoice.preLoaded(message.content.content);
                break;
            case RongIMClient.MessageType.ImageMessage:
                // do something...
                break;
            case RongIMClient.MessageType.DiscussionNotificationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.LocationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.RichContentMessage:
                // do something...
                break;
            case RongIMClient.MessageType.DiscussionNotificationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.InformationNotificationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.ContactNotificationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.ProfileNotificationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.CommandNotificationMessage:
                // do something...
                break;
            case RongIMClient.MessageType.CommandMessage:
                // do something...
                break;
            case RongIMClient.MessageType.UnknownMessage:
                // do something...
                break;
            default:
            // 自定义消息
            // do something...
        }
    }
});


function handleMsg(msg, client) {
    console.log(msg)
    $.closeModal();

    $.toast(msg.content.content)

    switch (msg.content.extra) {
        case 'NEW_SHIP':
            $.confirm("新配送订单,是否查看?", function () {
                $.router.load("/ship", true)
            }, function () {
                location.reload()
            });

            var audio = new Audio("/voice/notify.mp3");
            audio.play();


            break;
        case 'NEW_MASTER':
            $.confirm("新店铺订单,是否查看?", function () {
                $.router.load("/master/index/order", true)
            }, function () {
                location.reload()
            });

            var audio = new Audio("/voice/eleme.mp3");
            audio.play();

            break;
        case 'SHIP_START':
            $.alert("您的订单开始配送了", function () {
                location.reload()
            });
            break;
        case 'SHIP_END':
            $.alert("订单已经完成,欢迎评价!", function () {
                location.reload()
            });
            break;
    }

}